using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presentation.Contracts;
using Presentation;
using DataAccess;
using System.Collections.Generic;
using System.IO;
using Globals;

namespace ProjektZespolowy
{
    public partial class Art : Page, IArtView
    {

        public int UserId
        {
            get
            {
                if (Session[ContentNamesGlobals.SessionWithUserId] == null)
                    return 0;
                return int.Parse((string)Session[ContentNamesGlobals.SessionWithUserId]);

            }
        }

        public int GalleryId
        {
            get
            {
                if (Session[ContentNamesGlobals.GalleriId] == null)
                    return 0;
                else
                    return (int)Session[ContentNamesGlobals.GalleriId];
            }
            set
            {
                Session[ContentNamesGlobals.GalleriId] = value;
            }
        }
        
        public int ArtId
        {
            get
            {
                if (Session[ContentNamesGlobals.CurrentArtId] == null)
                    return 0;
                else
                    return (int)Session[ContentNamesGlobals.CurrentArtId];
            }
            set
            {
                Session[ContentNamesGlobals.CurrentArtId] = value;
            }
        }

        public int PageNumber
        {
            get
            {
                if (Session[ContentNamesGlobals.PageNumberSessionKey] == null)
                    return 0;
                else
                    return (int)Session[ContentNamesGlobals.PageNumberSessionKey];
            }
            set
            {
                Session[ContentNamesGlobals.PageNumberSessionKey] = value;
            }
        }

        public List<int> RatedArt
        {
            get
            {
                if (Session[ContentNamesGlobals.RatedArt] == null)
                    return null;
                else
                    return (List<int>)Session[ContentNamesGlobals.RatedArt];
            }
            set
            {
                Session[ContentNamesGlobals.RatedArt] = value;
            }
        }

        /// <summary>
        /// Presenter.
        /// </summary>
        private ArtPresenter presenter;

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new ArtPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string galleryIdString = Request.QueryString["galleryId"];
            string artIdString = Request.QueryString["artId"];

            if (!Page.IsPostBack)
            {
                int galleryId = 0;
                int.TryParse(galleryIdString, out galleryId);
                if (galleryId != 0)
                    GalleryId = galleryId;

                int artId = 0;
                int.TryParse(artIdString, out artId);
                if (artId != 0)
                    ArtId = artId;

                if((GalleryId != 0) || (ArtId != 0))
                {
                    presenter.FillPage();

                    //w url poprawny int
                    //presenter.ReadArtByArtId();
                    mvwPage.SetActiveView(vwPageOK);
                }
                else
                {
                    DisplayError("Podano niepoprawny adres");
                }
            }
        }

        public void DisplayError(string error)
        {
            mvwPage.SetActiveView(vwError);
            lblError.Text = error.Trim();
        }

        public void DisplayArtTypes(List<art_type> types)
        {
            ddlArtType.DataSource = types;
            ddlArtType.DataTextField = "name";
            ddlArtType.DataValueField = "id";
            ddlArtType.DataBind();
        }

        public void DisplayFotoView()
        {
            mvwArts.SetActiveView(vwFoto);
        }

        public void DisplayBinaryView()
        {
            mvwArts.SetActiveView(vwBinaries);
        }

        public void DisplayTextView()
        {
            mvwArts.SetActiveView(vwText);
        }

        public void DisplayFotoArtType()
        {
            ddlArtType.SelectedValue = ((int)ContentNamesGlobals.ArtType.grafika).ToString();
        }

        public void DisplayBinaryArtType()
        {
            ddlArtType.SelectedValue = ((int)ContentNamesGlobals.ArtType.binaria).ToString();
        }

        public void DisplayTextArtType()
        {
            ddlArtType.SelectedValue = ((int)ContentNamesGlobals.ArtType.tekst).ToString();
        }

        public void DisplayFotos(List<art> arts)
        {
            repFoto.DataSource = arts;
            repFoto.DataBind();
        }

        public void DisplayFoto(art a)
        {
            string path = "~//" + a.path;

            System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(path));

            int t = 500;

            int wspX = (int)(image.Width / t);
            int wspY = (int)(image.Height / t);

            int x = 0;
            int y = 0;

            if (wspX > wspY)
            {
                if (wspX != 0)
                {
                    x = (int)(image.Width / wspX);
                    y = (int)(image.Height / wspX);
                }
            }
            else
            {
                if (wspY != 0)
                {
                    x = (int)(image.Width / wspY);
                    y = (int)(image.Height / wspY);
                }
            }

            if (x > t)
                x = t;
            if (y > t)
                y = t;

            imgMain.ImageUrl = path;
            imgMain.Visible = true;
            imgMain.AlternateText = "foto";
            imgMain.Width = x;
            imgMain.Height = y;

            imgMain2.ImageUrl = path;
            imgMain2.Visible = true;
            imgMain2.AlternateText = "foto";
        }

        protected void ddlArtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            presenter.ArtTypeChanged();
        }

        public string GetMiniFoto(string bigFoto)
        {
            string extension = Path.GetExtension(bigFoto);
            bigFoto = bigFoto.Replace(extension, string.Empty);
            return bigFoto + "_mini" + extension;
        }

        protected void LeftClick(object sender, EventArgs e)
        {
            presenter.LeftClick();
        }

        protected void RightClick(object sender, EventArgs e)
        {
            presenter.RightClick();
        }

        public void EnableLeft(bool b)
        {
            imgBtnLeft.Visible = b;
        }

        public void EnableRight(bool b)
        {
            imgBtnRight.Visible = b;
        }

        protected void FotoButton_Command(object sender, CommandEventArgs e)
        {
            string fotoIdString = (string)e.CommandArgument;
            presenter.ChangeFoto(fotoIdString);
        }

        public void DisplayRateTypes(List<rate_type> rateTypes)
        {
            //ddlRate.DataSource = rateTypes;
            //ddlRate.DataTextField = "name";
            //ddlRate.DataValueField = "id";
            //ddlRate.DataBind();
        }

        protected void ddlRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            presenter.Rate();
        }

        public int GetRateId()
        {
            //return int.Parse(ddlRate.SelectedValue);
            return 0;
        }

        public void DisplayCountRate(int countRate)
        {
            lblCountRate.Text = countRate.ToString();
        }

        public void DisplayAverageRate(double averageRate)
        {
            lblAverageRate.Text = Convert.ToString(Math.Round(averageRate, 2));
        }

        public void DisplayVisit(int visits)
        {
            lblVisit.Text = visits.ToString();
        }

        public void EnableRate(bool can)
        {
            //ddlRate.Enabled = can;
            rateArt.ReadOnly = !can;
        }

        public void DisplayBinaries(List<art> binaries)
        {
            repBinaries.DataSource = binaries;
            repBinaries.DataBind();
        }

        public ContentNamesGlobals.ArtType GetSelectedArtType()
        {
            return (ContentNamesGlobals.ArtType)(int.Parse(ddlArtType.SelectedValue.ToString()));
        }

        public void DisplayFotoError(string error)
        {
            lblErrorFoto.Text = error;
        }

        public void DisplayBinaryError(string error)
        {
            lblErrorBinaries.Text = error;
        }

        public void DisplayTextError(string error)
        {
            lblErrorText.Text = error;
        }

        protected void BinaryButton_Command(object sender, CommandEventArgs e)
        {

            string binaryIdString = (string)e.CommandArgument;
            presenter.ChangeBinary(binaryIdString);

        }

        public void DisplayMainBinary(art binaryArt)
        {
            lblBinaryDescription.Text = binaryArt.description;
            lblBinaryName.Text = binaryArt.name;
            hplMainBinary.NavigateUrl = "~/" + binaryArt.path;
        }


        public void DisplayMainText(art a)
        {
            txtTextName.Text = a.name;
            txtTextDescription.Text = a.description;
            txtMainText.Text = a.text.Replace("\n\r","<br />").Replace("\n","<br />");
        }

        public void DisplayTexts(List<art> binaries)
        {
            repText.DataSource = binaries;
            repText.DataBind();
        }

        protected void TextButton_Command(object sender, CommandEventArgs e)
        {

            string IdString = (string)e.CommandArgument;
            presenter.ChangeText(IdString);

        }

        protected void RateArt_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
        {
            presenter.Rate(int.Parse(e.Value));
            rateArt.ReadOnly = true ;
        }
    }
}
